package cn.kgc.shiro03.service.impl;

import cn.kgc.shiro03.dto.RolePageDTO;
import cn.kgc.shiro03.entity.RolePers;
import cn.kgc.shiro03.entity.UserRole;
import cn.kgc.shiro03.mapper.RolePersMapper;
import cn.kgc.shiro03.mapper.UserRoleMapper;
import cn.kgc.shiro03.util.CodeMsg;
import cn.kgc.shiro03.util.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.shiro03.entity.Role;
import cn.kgc.shiro03.service.RoleService;
import cn.kgc.shiro03.mapper.RoleMapper;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired(required = false)
    private  RoleMapper roleMapper;
    @Autowired(required = false)
    private UserRoleMapper userRoleMapper;
    @Autowired(required = false)
    private RolePersMapper rolePersMapper;
    @Override
    public ResponseResult selectPage(RolePageDTO rolePageDTO) {

        Integer page = rolePageDTO.getPage();
        Integer limit = rolePageDTO.getLimit();
        String roleName = rolePageDTO.getRoleName();

        Page<Role> rolePage = new Page<>(page,limit);
        // 构建查询条件   搜索
        LambdaQueryWrapper<Role> lambda = new QueryWrapper<Role>().lambda();
        lambda.like(!StringUtils.isEmptyOrWhitespaceOnly(roleName), Role::getRoleName,roleName);
        roleMapper.selectPage(rolePage,lambda);

        //封装返回数据
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        responseResult.setData(rolePage.getRecords());
        responseResult.setCount(rolePage.getTotal());
        return responseResult;
    }

    @Override
    public ResponseResult deleteByIds(String ids) {
        ResponseResult responseResult = new ResponseResult();
        //将ids的字符串转换成 集合类型
        String[] strIds = ids.split(",");
        ArrayList<Integer> intIds = new ArrayList<>();
        for (String strId : strIds) {
            intIds.add(Integer.valueOf(strId));
        }

        //  判定用户角色关联表中是否有当前角色  user_role
        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("role_id", intIds);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);
        if(userRoles.size()>0){
            responseResult.setMsg(CodeMsg.ROLE_USED.getMsg());
            responseResult.setCode(CodeMsg.ROLE_USED.getCode());
        }else {
            // 删除角色信息
            roleMapper.deleteBatchIds(intIds);
            // 删除角色权限关联表中的信息  role_pers
            QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
            rolePersQueryWrapper.in("role_id", intIds);
            rolePersMapper.delete(rolePersQueryWrapper);

            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        }

        return responseResult;
    }

    @Override
    public ResponseResult selectPermissionByRoleId(Integer roleId) {

        QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
        rolePersQueryWrapper.eq("role_id", roleId);
        List<RolePers> rolePers = rolePersMapper.selectList(rolePersQueryWrapper);
        // 获取权限id
        ArrayList<Integer> perIds = new ArrayList<>();
        rolePers.forEach(rp->{
            perIds.add(rp.getPerId());
        });

        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(perIds);
        responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());

        return  responseResult;
    }

    @Override
    public ResponseResult addRolePermission(Integer roleId, String permissionIds) {

        // 删除roleId对应的权限
        QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
        rolePersQueryWrapper.eq("role_id", roleId);
        rolePersMapper.delete(rolePersQueryWrapper);

        //添加 roleId 对应最新权限
        String[] strPermissionId = permissionIds.split(",");
        for (String perId : strPermissionId) {
            RolePers rolePers = new RolePers();
            rolePers.setPerId(Integer.valueOf(perId));
            rolePers.setRoleId(roleId);
            rolePersMapper.insert(rolePers);
        }

        ResponseResult  responseResult = new ResponseResult();
        responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
        responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());

        return responseResult;
    }
}




